function select (e) {
  switch (e.value) {
    case '0':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text" + ' ' + 'hidden';
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd' + ' ' + 'hidden';
      document.getElementsByClassName("quest-num")[0].className = "quest-num" + ' ' + "hidden";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare" + ' ' + "hidden";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio" + ' ' + "hidden";
      document.getElementsByClassName("quest-check")[0].className = "quest-check" + ' ' + "hidden";
      break;
    case '1':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text";
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd' + ' ' + 'hidden';
      document.getElementsByClassName("quest-num")[0].className = "quest-num" + ' ' + "hidden";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare" + ' ' + "hidden";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio" + ' ' + "hidden";
      document.getElementsByClassName("quest-check")[0].className = "quest-check" + ' ' + "hidden";
      document.getElementsByClassName("put")[0].value = "";
      break;
    case '2':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text" + ' ' + 'hidden';
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd';
      document.getElementsByClassName("quest-num")[0].className = "quest-num" + ' ' + "hidden";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare" + ' ' + "hidden";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio" + ' ' + "hidden";
      document.getElementsByClassName("quest-check")[0].className = "quest-check" + ' ' + "hidden";
      document.getElementsByClassName("put")[1].value = "";
      break;
    case '3':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text" + ' ' + 'hidden';
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd' + ' ' + 'hidden';
      document.getElementsByClassName("quest-num")[0].className = "quest-num";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare" + ' ' + "hidden";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio" + ' ' + "hidden";
      document.getElementsByClassName("quest-check")[0].className = "quest-check" + ' ' + "hidden";
      document.getElementsByClassName("put")[2].value = "";
      break;
    case '4':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text" + ' ' + 'hidden';
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd' + ' ' + 'hidden';
      document.getElementsByClassName("quest-num")[0].className = "quest-num" + ' ' + "hidden";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio" + ' ' + "hidden";
      document.getElementsByClassName("quest-check")[0].className = "quest-check" + ' ' + "hidden";
      document.getElementsByClassName("put")[3].value = "";
      break;
    case '5':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text" + ' ' + 'hidden';
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd' + ' ' + 'hidden';
      document.getElementsByClassName("quest-num")[0].className = "quest-num" + ' ' + "hidden";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare" + ' ' + "hidden";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio";
      document.getElementsByClassName("quest-check")[0].className = "quest-check" + ' ' + "hidden";
      document.getElementsByClassName("put")[4].value = "";
      var questRadio = document.getElementsByClassName("quest-radio")[0];
      var radioTr = questRadio.children[0].children[0].children;
      var radioOption = document.getElementsByClassName("radio-option");
      radioOption[0].value = "";
      radioOption[1].value = "";
      let radioTrLenth = radioTr.length;
      if (radioTrLenth > 4) {
        for (var i = 3; i < radioTrLenth - 1; i++) {
          radioTr[3].remove();
        };
      };
      break;
    case '6':
      document.getElementsByClassName("quest-Text")[0].className = "quest-Text" + ' ' + 'hidden';
      document.getElementsByClassName("quest-pwd")[0].className = 'quest-pwd' + ' ' + 'hidden';
      document.getElementsByClassName("quest-num")[0].className = "quest-num" + ' ' + "hidden";
      document.getElementsByClassName("quest-textare")[0].className = "quest-textare" + ' ' + "hidden";
      document.getElementsByClassName("quest-radio")[0].className = "quest-radio" + ' ' + "hidden";
      document.getElementsByClassName("quest-check")[0].className = "quest-check";
      document.getElementsByClassName("put")[5].value = "";
      break;
    default:
  }
}


/**
 * 抓取新增问题按钮点击事件
 */
var num = 0;
document.getElementById("quest-add").onclick = function () {
  var questSubmit = document.createElement("input"); //name不能重复
  questSubmit.className = "hidden";
  var verify = document.createElement("span");
  verify.className = "verify-red";
  verify.innerHTML = "*";
  var questDel = document.createElement("input");
  questDel.type = "button";
  questDel.value = "刪除";
  questDel.className = "del";
  var group = document.getElementsByClassName("from-group")[0];
  var moveUp = document.createElement("input");
  var shiftDown = document.createElement("input");
  moveUp.type = "button";
  shiftDown.type = "button";
  moveUp.value = "上移";
  shiftDown.value = "下移";
  moveUp.className = "move-up";
  shiftDown.className = "shift-down";
  var selectType = document.getElementById("select-type"); //获取问题类型的下拉框元素
  var put = document.getElementsByClassName("put"); //获取每个问题元素
  var isreQuired = document.getElementById("isrequired"); //获取必选框元素
  for (var i = 0; i < selectType.length; i++) { //定义外循坏	
    if (selectType[i].selected && i != 0) { //选取状态	
      if (put[Number(selectType[i].value) - 1].value == "") { //判断题目是否为空
        alert("题目不能为空");
      } else {
        num++;
        document.getElementsByClassName("num")[0].value = num;
        switch (i) {
          case 1:
            questAddText(verify, questDel, group, moveUp, shiftDown, selectType, put[i - 1], isreQuired,
                    num, questSubmit);
            break;
          case 2:
            questAddPwd(verify, questDel, group, moveUp, shiftDown, selectType, put[i - 1], isreQuired,
                    num, questSubmit);
            break;
          case 3:
            questAddnum(verify, questDel, group, moveUp, shiftDown, selectType, put[i - 1], isreQuired,
                    num, questSubmit);
            break;
          case 4:
            num--;
            document.getElementsByClassName("num")[0].value = num;
            questAddTextarea(verify, questDel, group, moveUp, shiftDown, selectType, put[i - 1],
                    isreQuired);
            break;
          case 5:
            questAddRadio(verify, questDel, group, moveUp, shiftDown, selectType, put[i - 1], isreQuired,
                    num, questSubmit);
            break;
          case 6:
            questAddcheckbox(verify, questDel, group, moveUp, shiftDown, selectType, put[i - 1], isreQuired,
                    num, questSubmit);
            break;
          default:
        }
      }
    } else if (selectType[i].selected && i == 0) {
      alert("你还没有选择问题");
      break;
    }
  }
  spanDel();
  moveUps();
  shiftDowns();
};


/**
 * 這是題目刪除
 * 
 */
function spanDel () {
  var delspan = document.getElementsByClassName("del");
  for (var j = 0; j < delspan.length; j++) {
    delspan[j].onclick = function () {
      this.parentNode.parentNode.remove();
    };
  }
}
;


/**
 * 這是 text類型類型問題
 * 
 */
function questAddText (verify, questDel, group, moveUp, shiftDown, selectType, put, isreQuired, num, questSubmit) {
  var div = document.createElement("div");
  var p = document.createElement("p");
  var input = document.createElement("input");
  var hh = document.createElement("hr");
  questSubmit.name = "questSubmit" + num;
  questSubmit.value = put.value;
  div.className = "from-group";
  p.className = "topic-p";
  input.className = "contnet-text";
  input.type = "text";
  input.name = "questSubmitVal" + num;
  if (isrequired.checked) {
    input.required = "required";
    div.appendChild(hh);
    p.innerText = put.value;
    p.appendChild(verify);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    div.appendChild(input);
    div.appendChild(questSubmit);
    group.appendChild(div);

  } else {

    p.innerText = put.value;
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    div.appendChild(input);
    div.appendChild(questSubmit);
    group.appendChild(div);
  }
}
/**
 * 這是密碼類型問題
 * 
 */
function questAddPwd (verify, questDel, group, moveUp, shiftDown, selectType, put, isreQuired, num, questSubmit) {
  var div = document.createElement("div");
  var p = document.createElement("p");
  var input = document.createElement("input");
  var hh = document.createElement("hr");
  questSubmit.name = "questSubmit" + num;
  questSubmit.value = put.value;
  div.className = "from-group";
  p.className = "topic-p";
  input.className = "contnet-text";
  input.type = "password";
  input.name = "questSubmitVal" + num;
  if (isreQuired.checked) {
    input.required = "required";
    p.innerText = document.getElementsByClassName("put")[1].value;
    p.appendChild(verify);
    div.appendChild(p);
    div.appendChild(input);
    div.appendChild(questSubmit);
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    group.appendChild(div);
  } else {
    p.innerText = document.getElementsByClassName("put")[1].value;
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    div.appendChild(input);
    div.appendChild(questSubmit);
    div.appendChild(hh);
    group.appendChild(div);
  }

}
/**
 * 這是文字框類型問題
 * 
 */
function questAddTextarea (verify, questDel, group, moveUp, shiftDown, selectType, put, isreQuired) {
  var div = document.createElement("div");
  var p = document.createElement("p");
  var textareaText = document.createElement("textarea");
  var hh = document.createElement("hr");
  div.className = "from-group";
  p.className = "topic-p";
  textareaText.className = "textarea-text";
  textareaText.value = put.value;
  if (isrequired.checked) {
    textareaText.required = "required";
    p.appendChild(verify);
    div.appendChild(p);
    div.appendChild(textareaText);
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    group.appendChild(div);
  } else {


    div.appendChild(p);
    div.appendChild(textareaText);
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    group.appendChild(div);
  }
}


function questAddnum (verify, questDel, group, moveUp, shiftDown, selectType, put, isreQuired, num, questSubmit) {
  var div = document.createElement("div");
  var p = document.createElement("p");
  var input = document.createElement("input");
  var hh = document.createElement("hr");
  var maxrequired = document.getElementsByClassName("max-required")[0];
  var minrequired = document.getElementsByClassName("min-required")[0];

  questSubmit.name = "questSubmit" + num;
  questSubmit.value = put.value;
  div.className = "from-group";
  p.className = "topic-p";
  input.className = "contnet-text";
  input.type = "number";
  input.name = "questSubmitVal" + num;
  if (isrequired.checked) {
    input.required = "required";
    div.appendChild(hh);
    p.innerText = put.value;
    p.appendChild(verify);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    div.appendChild(input);
    div.appendChild(questSubmit);
    group.appendChild(div);

  } else {
    p.innerText = put.value;
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    div.appendChild(input);
    div.appendChild(questSubmit);
    group.appendChild(div);
  }

  if (maxrequired.checked) {
    //加max验证

    var maxNum = document.getElementsByClassName("max-num")[0].value;
    alert(maxNum);
    input.max = maxNum;
  }
  if (minrequired.checked) {
    //加min验证
    var minnum = document.getElementsByClassName("min-num")[0].value;
    input.min = minnum;
  }
}







/**
 * 這是radio新增選項
 * 
 */
document.getElementById("optin-add-radio").onclick = function () {
  var tr = document.createElement("tr");
  var td1 = document.createElement("td");
  var td2 = document.createElement("td");
  var radio = document.createElement("input");
  radio.type = "radio";
  var radioOption = document.createElement("input");
  radioOption.type = "text";
  radioOption.className = "radio-option";

  tr.appendChild(td1);
  tr.appendChild(td2);
  td2.appendChild(radio);
  td2.appendChild(radioOption);
  var parentTr = this.parentNode.parentNode.parentNode; //父节点
  var referenceTr = this.parentNode.parentNode.parentNode.lastElementChild; //参照节点
  parentTr.insertBefore(tr, referenceTr);

}
/**
 * 這是checkbox新增選項
 * 
 */
document.getElementById("optin-add-checkbox").onclick = function () {
  var tr = document.createElement("tr");
  var td1 = document.createElement("td");
  var td2 = document.createElement("td");
  var check = document.createElement("input");
  check.type = "checkbox";
  var checkOption = document.createElement("input");
  checkOption.type = "text";
  checkOption.className = "check-option";

  tr.appendChild(td1);
  tr.appendChild(td2);
  td2.appendChild(check);
  td2.appendChild(checkOption);
  var parentTr = this.parentNode.parentNode.parentNode; //父节点
  var referenceTr = this.parentNode.parentNode.parentNode.lastElementChild; //参照节点
  parentTr.insertBefore(tr, referenceTr);

}


function questAddRadio (verify, questDel, group, moveUp, shiftDown, selectType, put, isreQuired, num, questSubmit) {

  var radioOption = document.getElementsByClassName("radio-option");

  var labelRadio = new Array();
  for (var i = 0; i < radioOption.length; i++) {
    var inputRadio = document.createElement("input");
    var label = document.createElement("label");
    inputRadio.type = "radio";
    inputRadio.required = "required";
    inputRadio.value = radioOption[i].value;
    inputRadio.name = "questSubmitVal" + num;
    label.appendChild(inputRadio);
    label.appendChild(document.createTextNode(radioOption[i].value));
    labelRadio[i] = label;
  }

  var div = document.createElement("div");
  var p = document.createElement("p");
  var hh = document.createElement("hr");
  questSubmit.name = "questSubmit" + num;
  questSubmit.value = put.value;
  div.className = "from-group";
  p.className = "topic-p";

  if (isrequired.checked) {

    div.appendChild(hh);
    p.innerText = put.value;
    p.appendChild(verify);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);

    for (var j = 0; j < labelRadio.length; j++) {
      div.appendChild(labelRadio[j]);
    }

    div.appendChild(questSubmit);
    group.appendChild(div);

  } else {
    p.innerText = put.value;
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    for (var j = 0; j < labelRadio.length; j++) {
      div.appendChild(labelRadio[j]);
    }
    div.appendChild(questSubmit);
    group.appendChild(div);
  }


}

function questAddcheckbox (verify, questDel, group, moveUp, shiftDown, selectType, put, isreQuired, num, questSubmit) {

  var checkOption = document.getElementsByClassName("check-option");
  var labelCheck = new Array();
  for (var i = 0; i < checkOption.length; i++) {
    var inputCheck = document.createElement("input");
    var label = document.createElement("label");
    inputCheck.type = "checkbox";
    inputCheck.required = "required";
    inputCheck.value = checkOption[i].value;
    inputCheck.name = "questSubmitVal" + num;
    label.appendChild(inputCheck);
    label.appendChild(document.createTextNode(checkOption[i].value));
    labelCheck[i] = label;
  }
  var div = document.createElement("div");
  var p = document.createElement("p");
  var hh = document.createElement("hr");
  questSubmit.name = "questSubmit" + num;
  questSubmit.value = put.value;
  div.className = "from-group";
  p.className = "topic-p";

  if (isrequired.checked) {
    div.appendChild(hh);
    p.innerText = put.value;
    p.appendChild(verify);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);

    for (var j = 0; j < labelCheck.length; j++) {
      div.appendChild(labelCheck[j]);
    }

    div.appendChild(questSubmit);
    group.appendChild(div);

  } else {
    p.innerText = put.value;
    div.appendChild(hh);
    p.appendChild(moveUp);
    p.appendChild(shiftDown);
    p.appendChild(questDel);
    div.appendChild(p);
    for (var j = 0; j < labelCheck.length; j++) {
      div.appendChild(labelCheck[j]);
    }
    div.appendChild(questSubmit);
    group.appendChild(div);
  }


}



function moveUps () {
  var moveUp = document.getElementsByClassName("move-up");
  for (var j = 0; j < moveUp.length; j++) {
    moveUp[j].onclick = function () {
      var parentGroup = this.parentNode.parentNode.parentNode; //父节点
      var referenceGroup = this.parentNode.parentNode.previousElementSibling; //参照节点
      parentGroup.insertBefore(this.parentNode.parentNode, referenceGroup);
    };
  }
}
function shiftDowns () {
  var shiftDown = document.getElementsByClassName("shift-down");
  for (var j = 0; j < shiftDown.length; j++) {
    shiftDown[j].onclick = function () {
      var parentGroup = this.parentNode.parentNode.parentNode; //父节点
      var referenceGroup = this.parentNode.parentNode.nextElementSibling.nextElementSibling; //参照节点
      parentGroup.insertBefore(this.parentNode.parentNode, referenceGroup);

    };
  }
}
